﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Text;

namespace OpenSource.Web
{
	[DefaultProperty( "SelectedFont" )]
	[ToolboxData( "<{0}:FontPicker runat=server></{0}:FontPicker>" )]
	public class FontPicker : WebControl
	{
		public FontPicker()
		{
			selectedFont = new Font( "Tahoma", 9 );
		}
		private Font selectedFont;

		[Bindable( true )]
		[Category( "Behavior" )]
		[DefaultValue( "Tahoma" )]
		public Font SelectedFont
		{
			get { return selectedFont; }
			set { selectedFont = value; }
		}

		[Bindable( true )]
		[Category( "Appearance" )]
		[DefaultValue( true )]
		[Localizable( true )]
		public bool RightToLeft
		{
			get
			{
				return (ViewState["RightToLeft"] == null ? true : (bool)ViewState["RightToLeft"]);
			}
			set { ViewState["RightToLeft"] = value; }
		}
		[Bindable( true )]
		[Category( "Behavior" )]
		[DefaultValue( false )]
		[Localizable( true )]
		public bool ApplyFont
		{
			get
			{
				return (ViewState["ApplyFont"] == null ? false : (bool)ViewState["ApplyFont"]);
			}
			set { ViewState["ApplyFont"] = value; }
		}
		[Bindable( true )]
		[Category( "Behavior" )]
		[DefaultValue( 1 )]
		[Localizable( true )]
		public float FontScale
		{
			get
			{
				return (ViewState["FontScale"] == null ? 1f : (float)ViewState["FontScale"]);
			}
			set
			{
				if ( value == 1 )
					ViewState.Remove( "FontScale" );
				else
					ViewState["FontScale"] = value;
			}
		}
		[Bindable( false )]
		[Category( "Misc" )]
		[DefaultValue( "" )]
		[Localizable( false )]
		public string ClientJSID
		{
			get
			{
				string s = (string)ViewState["ClientJSID"];
				return (s == null) ? "" : s;
			}
			set { ViewState["ClientJSID"] = value; }
		}
		//[Bindable( true )]
		//[Category( "Behavior" )]
		//[DefaultValue( false )]
		//[Localizable( false )]
		//public bool ClientSideOnly
		//{
		//    get
		//    {
		//        return (ViewState["ClientSideOnly"] == null) ? false : (bool)ViewState["ClientSideOnly"];
		//    }
		//    set { ViewState["ClientSideOnly"] = value; }
		//}
		[Bindable( true )]
		[Category( "Appearance" )]
		[DefaultValue( "انتخاب فونت" )]
		[Localizable( true )]
		public string FontDialogTitle
		{
			get
			{
				string s = (string)ViewState["Title"];
				return (s == null) ? "انتخاب فونت" : s;
			}
			set { ViewState["Title"] = value; }
		}

		protected override HtmlTextWriterTag TagKey
		{
			get { return HtmlTextWriterTag.Span; }
		}
		protected override void AddAttributesToRender( HtmlTextWriter writer )
		{
			base.AddAttributesToRender( writer );
			writer.AddStyleAttribute( HtmlTextWriterStyle.Direction, RightToLeft ? "rtl" : "ltr" );
			writer.AddAttribute( HtmlTextWriterAttribute.Dir, RightToLeft ? "rtl" : "ltr" );
		}

		protected override void OnPreRender( EventArgs e )
		{
			string id = ClientJSID;
			if ( id == "" )
				id = ClientID + "js";
			Page.ClientScript.RegisterClientScriptResource( GetType(), "OpenSource.Web.FontPicker.script.js" );
			if ( !Page.ClientScript.IsClientScriptBlockRegistered( GetType(), "fontFamilies" ) )
			{
				InstalledFontCollection ifc = new InstalledFontCollection();
				string js = "\r\nWebFontPicker.fontFamilies = new Array();\r\n";
				foreach ( FontFamily f in ifc.Families )
					js += "WebFontPicker.fontFamilies.push('" + f.Name + "');\r\n";
				Page.ClientScript.RegisterClientScriptBlock( GetType(), "fontFamilies", js, true );
			}
			string s = "var " + id + " = new WebFontPicker( '" + ClientID + "','" + FontDialogTitle
				+ "','" + selectedFont.Name + "'," + selectedFont.SizeInPoints.ToString()
				+ "," + selectedFont.Bold.ToString().ToLower() + "," + selectedFont.Italic.ToString().ToLower() + ","
				+ selectedFont.Underline.ToString().ToLower() + "," + selectedFont.Strikeout.ToString().ToLower() + ");";
			s += "\r\n" + id + ".applyFont = " + ApplyFont.ToString().ToLower();
			s += "\r\n" + id + ".scale = " + FontScale.ToString();
			Page.ClientScript.RegisterClientScriptBlock( GetType(), ClientID, s, true );
			base.OnPreRender( e );
		}
		protected override void RenderContents( HtmlTextWriter output )
		{
			if ( ApplyFont )
			{
				output.AddStyleAttribute( HtmlTextWriterStyle.FontFamily, selectedFont.FontFamily.Name );
				output.AddStyleAttribute( HtmlTextWriterStyle.FontSize, selectedFont.SizeInPoints.ToString() + "pt;" );
				output.AddStyleAttribute( HtmlTextWriterStyle.FontStyle, (selectedFont.Style & FontStyle.Italic) == FontStyle.Italic ? "italic" : "normal" );
				output.AddStyleAttribute( HtmlTextWriterStyle.FontWeight, (selectedFont.Style & FontStyle.Bold) == FontStyle.Bold ? "bold" : "normal" );

				string s = ((selectedFont.Style & FontStyle.Underline) == FontStyle.Underline ? "underline" : "");
				s += " " + ((selectedFont.Style & FontStyle.Strikeout) == FontStyle.Strikeout ? "line-through" : "");
				output.AddStyleAttribute( HtmlTextWriterStyle.TextDecoration, s.Trim() == "" ? "none" : s );
			}
			output.AddAttribute( HtmlTextWriterAttribute.Id, ClientID + "_fel" );
			output.RenderBeginTag( HtmlTextWriterTag.Span );

			string st = ((selectedFont.Style & FontStyle.Bold) == FontStyle.Bold) ? "Bold" : "";
			if ( (selectedFont.Style & FontStyle.Italic) == FontStyle.Italic )
			{
				if ( st != "" )
					st += ',';
				st += "Italic";
			}
			if ( (selectedFont.Style & FontStyle.Underline) == FontStyle.Underline )
			{
				if ( st != "" )
					st += ',';
				st += "Underline";
			}
			if ( (selectedFont.Style & FontStyle.Strikeout) == FontStyle.Strikeout )
			{
				if ( st != "" )
					st += ',';
				st += "Strikeout";
			}

			output.WriteEncodedText( selectedFont.FontFamily.Name + "; " + selectedFont.SizeInPoints.ToString() + "pt"
				+ (st == "" ? "" : "; " + st) );
			output.RenderEndTag();
			output.RenderBeginTag( HtmlTextWriterTag.Span );
			output.Write( "&nbsp;&nbsp;" );
			output.AddAttribute( HtmlTextWriterAttribute.Value, "..." );
			st = ClientJSID;
			if ( st == "" )
				st = ClientID + "js";
			output.AddAttribute( HtmlTextWriterAttribute.Type, "button" );
			output.AddAttribute( HtmlTextWriterAttribute.Onclick, st + ".show();" );
			output.RenderBeginTag( HtmlTextWriterTag.Input );
			output.RenderEndTag();
			output.AddAttribute( HtmlTextWriterAttribute.Id, ClientID + "_arg" );
			output.AddAttribute( HtmlTextWriterAttribute.Name, ClientID + "_arg" );
			output.AddAttribute( HtmlTextWriterAttribute.Type, "hidden" );
			string[] ar = new string[6];
			ar[0] = selectedFont.Name;
			ar[1] = selectedFont.SizeInPoints.ToString();
			ar[2] = selectedFont.Bold ? "true" : "";
			ar[3] = selectedFont.Italic ? "true" : "";
			ar[4] = selectedFont.Underline ? "true" : "";
			ar[5] = selectedFont.Strikeout ? "true" : "";
			output.AddAttribute( HtmlTextWriterAttribute.Value, string.Join( "$", ar ) );
			output.RenderBeginTag( HtmlTextWriterTag.Input );
			output.RenderEndTag();
			output.RenderEndTag();
		}

		protected override void OnInit( EventArgs e )
		{
			base.OnInit( e );
			if ( Page.IsPostBack )//&& !ClientSideOnly )
			{
				try
				{
					string[] args = Page.Request.Params[ClientID + "_arg"].Split( '$' );
					FontStyle fs = FontStyle.Regular;
					if ( args[2] == "true" )
						fs |= FontStyle.Bold;
					if ( args[3] == "true" )
						fs |= FontStyle.Italic;
					if ( args[4] == "true" )
						fs |= FontStyle.Underline;
					if ( args[5] == "true" )
						fs |= FontStyle.Strikeout;
					selectedFont = new Font( args[0], float.Parse( args[1] ), fs );
				}
				catch { }
			}
		}
	}
}
